package com.example.za.account.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.za.account.R;
import com.example.za.account.adapter.CommonAdapter;
import com.example.za.account.application.AccountApplication;
import com.example.za.account.dao.ExpenseCatDao;
import com.example.za.account.dao.IncomeCatDao;
import com.example.za.account.dao.IncomeDao;
import com.example.za.account.model.ExpenseCat;
import com.example.za.account.model.IncomeCat;
import com.example.za.account.utils.Constant;
import com.example.za.account.utils.L;
import com.example.za.account.utils.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CategoryActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    @BindView(R.id.icon_add_category)
    ImageView iconAddCategory;
    @BindView(R.id.et_add_category)
    EditText etAddCategory;
    @BindView(R.id.grid_add_category)
    GridView gridAddCategory;
    private int mResId;
    private ExpenseCat mExpenseCat;
    private IncomeCat mIncomeCat;
    private String mType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category);
        ButterKnife.bind(this);
        setTitle(getString(R.string.add_category));
        showBackwardView(true);
        showForwardView(true);
        Parcelable extra = getIntent().getParcelableExtra(Constant.UPDATE_CAT);
        if (extra != null) {
            setTitle(getString(R.string.modify_category));
            if (extra instanceof IncomeCat) {
                mType = Constant.TYPE_INCOME;
                mIncomeCat = (IncomeCat) extra;
                etAddCategory.setText(mIncomeCat.getmName());
                iconAddCategory.setImageResource(mIncomeCat.getmImageId());
                mResId = mIncomeCat.getmImageId();
            } else {
                mType = Constant.TYPE_EXPENSE;
                mExpenseCat = (ExpenseCat) extra;
                etAddCategory.setText(mExpenseCat.getmName());
                iconAddCategory.setImageResource(mExpenseCat.getmImageId());
                mResId = mExpenseCat.getmImageId();
            }
        } else {
            mResId = R.drawable.icon_shouru_type_qita;
            mType = getIntent().getStringExtra(Constant.TYPE_CATEGORY);
            setTitle(getString(R.string.add_category));
        }
        CommonAdapter commonAdapter = new CommonAdapter<Integer>(initData(), R.layout.item_add_category) {
            @Override
            public void bindView(ViewHolder holder, Integer obj) {
                holder.setImageResource(R.id.item_add_category, obj);
            }
        };
        gridAddCategory.setAdapter(commonAdapter);
        gridAddCategory.setOnItemClickListener(this);


    }

    private List<Integer> initData() {
        int resId[] = {R.drawable.icon_zhichu_type_canyin, R.drawable.maicai, R.drawable.icon_zhichu_type_yanjiuyinliao,
                R.drawable.icon_zhichu_type_shuiguolingshi, R.drawable.baojian, R.drawable.ad, R.drawable.anjie,
                R.drawable.baobao, R.drawable.baoxian, R.drawable.baoxiao, R.drawable.chuanpiao, R.drawable.daoyou,
                R.drawable.dapai, R.drawable.dianfei, R.drawable.dianying, R.drawable.fangdai, R.drawable.fangzu,
                R.drawable.fanka, R.drawable.feijipiao, R.drawable.fuwu, R.drawable.gonggongqiche, R.drawable.haiwaidaigou,
                R.drawable.huankuan, R.drawable.huazhuangpin, R.drawable.huochepiao, R.drawable.huwaishebei,
                R.drawable.icon_add_1, R.drawable.icon_add_2, R.drawable.icon_add_3, R.drawable.icon_add_4,
                R.drawable.icon_add_5, R.drawable.icon_add_6, R.drawable.icon_add_7, R.drawable.icon_add_8,
                R.drawable.icon_add_9, R.drawable.icon_add_10, R.drawable.icon_add_11, R.drawable.icon_add_12,
                R.drawable.icon_add_13, R.drawable.icon_add_14, R.drawable.icon_add_15, R.drawable.icon_add_16,
                R.drawable.icon_add_17, R.drawable.icon_add_18, R.drawable.icon_add_19, R.drawable.icon_add_20,
                R.drawable.icon_shouru_type_gongzi, R.drawable.icon_shouru_type_hongbao, R.drawable.icon_shouru_type_jiangjin,
                R.drawable.icon_shouru_type_jianzhiwaikuai, R.drawable.icon_shouru_type_jieru,
                R.drawable.icon_shouru_type_linghuaqian, R.drawable.icon_shouru_type_shenghuofei,
                R.drawable.icon_shouru_type_touzishouru, R.drawable.icon_zhichu_type_baoxiaozhang, R.drawable.icon_zhichu_type_canyin,
                R.drawable.icon_zhichu_type_chongwu, R.drawable.icon_zhichu_type_gouwu, R.drawable.icon_zhichu_type_jiaotong,
                R.drawable.icon_zhichu_type_jiechu, R.drawable.icon_zhichu_type_jujia, R.drawable.icon_zhichu_type_meirongjianshen,
                R.drawable.icon_zhichu_type_renqingsongli, R.drawable.icon_zhichu_type_shoujitongxun,
                R.drawable.icon_zhichu_type_shuji, R.drawable.icon_zhichu_type_taobao, R.drawable.icon_zhichu_type_yanjiuyinliao,
                R.drawable.icon_zhichu_type_yiban, R.drawable.icon_zhichu_type_yule, R.drawable.jiushui,
                R.drawable.juechu, R.drawable.kuzi, R.drawable.lifa, R.drawable.lingqian, R.drawable.lingshi,
                R.drawable.lvyoudujia, R.drawable.majiang, R.drawable.mao, R.drawable.naifen, R.drawable.party,
                R.drawable.quxian, R.drawable.richangyongpin, R.drawable.shuifei, R.drawable.shumachanpin,
                R.drawable.sijiache, R.drawable.tingchefei, R.drawable.tuikuan, R.drawable.wanfan, R.drawable.wangfei,
                R.drawable.wanggou, R.drawable.wanju, R.drawable.weixiubaoyang, R.drawable.wuye, R.drawable.xianjin,
                R.drawable.xiaochi, R.drawable.xiaojingjiazhang, R.drawable.xiezi, R.drawable.xinyongkahuankuan,
                R.drawable.xizao, R.drawable.xuefei, R.drawable.yan, R.drawable.yaopinfei, R.drawable.yifu, R.drawable.yinhangshouxufei,
                R.drawable.yiwaiposun, R.drawable.yiwaisuode, R.drawable.youfei, R.drawable.youxi, R.drawable.yuegenghuan,
                R.drawable.yundongjianshen, R.drawable.zhifubao, R.drawable.zhongfan, R.drawable.zhuanzhang,
                R.drawable.zhusu, R.drawable.zuojifei};
        List<Integer> resIds = new ArrayList<>(resId.length);
        for (int aResId : resId) {
            resIds.add(aResId);
        }
        return resIds;
    }

    @Override
    protected void onForword() {

        //获取类别名称
        String name = etAddCategory.getText().toString().trim();
        if (!TextUtils.isEmpty(name)) {
            if (TextUtils.equals(mType, Constant.TYPE_INCOME)) {
                IncomeCatDao incomeCatDao = new IncomeCatDao(this);
                if (mIncomeCat != null) {
                    //如果更新成功，提示用户成功，并关闭窗口
                    if (incomeCatDao.update(new IncomeCat(mIncomeCat.getmId(), name,
                            mResId, AccountApplication.sUser))) {
//                        IncomeDao incomeDao = new IncomeDao(this);
//                        Map<String, Object> map = new HashMap<>();
//                        IncomeCat incomeCat = new IncomeCat(mIncomeCat.getmId(), name,
//                                mResId, AccountApplication.sUser);
//                        map.put("AScategoryId", incomeCat);
//                        L.e("xiugai后查询:"+String.valueOf(incomeDao.existsInfo(map)));

                        T.showShort(this, getString(R.string.update_succeed));
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        T.showShort(this, getString(R.string.update_fail));
                    }
                } else {
                    if (incomeCatDao.addCategory(new IncomeCat(mResId, name, AccountApplication.sUser))) {
                        T.showShort(this, getString(R.string.save_succeed));
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        T.showShort(this, getString(R.string.save_fail));
                    }
                }
            } else if (TextUtils.equals(mType, Constant.TYPE_EXPENSE)) {
                ExpenseCatDao expenseCatDao = new ExpenseCatDao(this);
                if (mExpenseCat != null) {
                    if (expenseCatDao.update(new ExpenseCat(mExpenseCat.getmId(),
                            name, mResId, AccountApplication.sUser))) {
                        T.showShort(this, getString(R.string.update_succeed));
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        T.showShort(this, getString(R.string.update_fail));
                    }
                } else {
                    if (expenseCatDao.addCategory(new ExpenseCat(mResId, name, AccountApplication.sUser))) {
                        T.showShort(this, getString(R.string.save_succeed));
                        setResult(RESULT_OK);
                        finish();
                    } else {
                        T.showShort(this, getString(R.string.save_fail));
                    }
                }
            }
        } else {
            T.showShort(this, getString(R.string.input_category_name));
        }
    }


    @Override
    protected Activity getSubActivity() {
        return this;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        mResId = (int) parent.getItemAtPosition(position);
        iconAddCategory.setImageResource(mResId);
    }

}
